# Here we are calling the "append" method of the list class.
icb_list = ['QRL','QNQ','QU9','QSL'] # a list object
icb_list.append('QNX') # calling the method and passing a parameter
print(icb_list)['QRL', 'QNQ', 'QU9', 'QSL', 'QNX']
# Here we are calling the "append" method of the list class.
icb_list = ['QRL','QNQ','QU9','QSL'] # a list object
icb_list.append('QNX') # calling the method and passing a parameter
print(icb_list)['QRL', 'QNQ', 'QU9', 'QSL', 'QNX']
- Classes - Objects
- Encapsulation - Inheritance - Polymorphism - Abstraction
A diagram representing a class at the top and object instances created from the class
__init__ suggests, these are the initial valuesPython code for creating a class
# Use the "class" keyword. Class names should start with a
# capital letter
class HealthProfessional:
# Class attributes go here. The value of this attribute
# will be the same for all object instances
daily_capacity = 7.5 # number of working hours per day
# Constructor method. It always contains "self" followed by
# the parameters passed at instantiation
def __init__(self, assignment_number, division, department):
self.assignment_number = assignment_number
self.division = division # Instance attributes
self.department = department
# Class methods. Always have at least "self" as a parameter,
# followed by any parameters passed to the object
def treat_patient(self,patient_id):
print(f'Health professional {self.assignment_number} treated patient {patient_id}')
# Let's instantiate a HealthProfessional
doctor_duggee = HealthProfessional(12345,"A","Surgery")
# Using the .treat_patient() method
doctor_duggee.treat_patient("Betty")
# Accessing the object's attributes
print(f'Health Professional {doctor_duggee.assignment_number} works in the {doctor_duggee.department} department')Health professional 12345 treated patient Betty
Health Professional 12345 works in the Surgery department
A parent class (green) and two different child classes (blue and orange), each with objects created from them
Python code for creating a child class from a parent class
# Create the parent class
class HealthProfessional:
daily_capacity = 7.5
def __init__(self, assignment_number, division, department):
self.assignment_number = assignment_number
self.division = division
self.department = department
def treat_patient(self,patient_id):
print(f'Health professional {self.assignment_number} treated patient {patient_id}')
# Create the child class
# The parent class goes in parentheses after the child class name
class Doctor(HealthProfessional):
def __init__(self,assignment_number,division,department,seniority):
self.seniority = seniority
super().__init__(assignment_number,division,department)
def discharge_patient(self,patient_id):
print(f'Doctor {self.assignment_number} discharged patient {patient_id}')
# The "treat_patient" method is inherited from HealthProfessional
doctor_duggee = Doctor(12345,"A","Sugery","Consultant")
doctor_duggee.treat_patient("Betty")
doctor_duggee.discharge_patient("Betty")
print(f'Doctor {doctor_duggee.assignment_number} is a {doctor_duggee.seniority}')Health professional 12345 treated patient Betty
Doctor 12345 discharged patient Betty
Doctor 12345 is a Consultant
df = pd.DataFrame(data) <– Instantiating a dataframe objectdf.head(), df.describe(), df.drop() <– calling methodsfrom sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
models = [LogisticRegression(), SVC()]
for model in models:
model.fit(X_train, y_train) # Both have .fit() method
preds = model.predict(X_test) # Both have .predict() method
RealPython: Object-Oriented Programming (OOP) in Python
freeCodeCamp - Intro to Object-Oriented Programming
HSMA - Introduction to Python Programming Part 3 (including OOP)
HSMA - Introduction to Discrete Event Simulation
Well done, Squirrels, you’ve earned your Object-Oriented Programming Badge!
Edward Chick // Introduction to Object-Oriented Programming // 2025-02-28